package cn.weakup.thread.question._synchronized;

/**
 * @author Dawn
 * @version 1.0.0
 * @desc
 * @date 2022/5/10
 */
public class SynchronizedTest {

    public static void main(String[] args) {
        //testNoLock();
        //testLockMethod();
        //testLockStaticMethodOrClass();
        testLockMethodBlock();
    }

    private static void testLockMethodBlock(){
        final LockMethodBlock test = new LockMethodBlock();

        new Thread(()->{
            test.method1();
        }).start();

        new Thread(()->{
            test.method2();
        }).start();
    }

    private static void testLockStaticMethodOrClass(){
        new Thread(()->{
            LockStaticMethodOrClass.method1();
        }).start();

        new Thread(()->{
            LockStaticMethodOrClass.method2();
        }).start();
    }

    private static void testNoLock(){
        final NoLock test = new NoLock();

        new Thread(()->{
            test.method1();
        }).start();

        new Thread(()->{
            test.method2();
        }).start();
    }

    static class NoLock{
        public void method1(){
            System.out.println("Method 1 start");
            try {
                System.out.println("Method 1 execute");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Method 1 end");
        }

        public void method2(){
            System.out.println("Method 2 start");
            try {
                System.out.println("Method 2 execute");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Method 2 end");
        }
    }

    private static void testLockMethod(){
        final LockMethod test = new LockMethod();

        new Thread(()->{
            test.method1();
        }).start();

        new Thread(()->{
            test.method2();
        }).start();
    }



}
